//建造炮台
import Global from "store"
cc.Class({
    extends: cc.Component,

    properties: {
        P_cannonPrefab: {
            default: null,
            type: cc.Prefab,
            tooltip: "炮台Prefab"

        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.cannonList = []
        for (let i = 0; i < Global.cannonData.length; i++) {
            this.cannonList[i] = {}
            this.cannonList[i].x = Global.cannonData[i].x
            this.cannonList[i].y = Global.cannonData[i].y
            this.cannonList[i].canMake = true
        }
        Global.cannonBuild = this
    },
    getCanMakeIndex: function () {
        for (let i = 0; i < this.cannonList.length; i++) {
            if (this.cannonList[i].canMake) {
                return i;
            }
        }
        return null;
    },
    cannonBuild: function (index) {
        console.log(index)
        if (this.cannonList[index] == null) return;
        var node = cc.instantiate(this.P_cannonPrefab);
        this.node.addChild(node);
        node.x = this.cannonList[index].x * Global.main.P_blockWidth + Global.main.P_blockWidth / 2;
        node.y = -this.cannonList[index].y * Global.main.P_blockHeight - Global.main.P_blockHeight / 2;
        this.cannonList[index].canMake = false;
        this.cannonList[index].cannon = node;

    }

});